package api

import (
	"FinancialManagement/src/base/baselog"
	"FinancialManagement/src/base/mess"
	"FinancialManagement/src/business/util"
	"github.com/gin-gonic/gin"
	"github.com/go-session/session"
	"go.uber.org/zap"
	"net/http"
	"time"
)

// GetCaptchaImage
// @Description: 获取图形验证码
// @param c
func GetCaptchaImage(c *gin.Context) {
	// 获取验证码
	idKey, base64string, err := util.GetCaptcha("string")
	if err != nil {
		c.JSON(http.StatusOK, mess.ResErrMess(90010050014, "获取图形验证码错误"))
	} else {
		data := map[string]interface{}{}
		data["id"] = idKey
		data["base64img"] = base64string
		c.JSON(http.StatusOK, mess.ResMessOK(data))
	}
}

// VerifyCaptcha
// @Description: 验证图形验证码是否正确
// @param c
func VerifyCaptcha(c *gin.Context) {
	idKey := c.DefaultPostForm("idKey", "") // 验证码标记号
	num := c.DefaultPostForm("num", "")     // 需要验证的验证码
	if idKey == "" || num == "" {
		c.JSON(http.StatusOK, mess.ResErrMess(80010050001, "请求参数缺失，请确认"))
		return
	}
	t := util.VerifyCaptcha(idKey, num) // 确认验证码是否正确
	if t {
		store, _ := session.Start(c.Request.Context(), c.Writer, c.Request)
		baselog.Logger.Info(store.SessionID())
		ru, ok := store.Get("ReturnUri")
		baselog.Logger.Info("session会话情况？", zap.Bool("是否获取成功：", ok), zap.Any("test", ru))
		// 存储本次验证成功的时间戳
		store.Set(CAPTCHA_KEY, time.Now().Second())
		store.Save()
	}
	c.JSON(http.StatusOK, mess.ResMessOK(t))
}
